//+++++++++++++++++++++++++++++++++
// ReShade effect file
// Crosire, McFly, CeeJayDK
//edited by Quant
//+++++++++++++++++++++++++++++++++


//+++++++++++++++++++++++++++++++++
//	Depth Of Field
//+++++++++++++++++++++++++++++++++
#define DOF false
#define	Far 1
#define Pow 10
#define Quality 4
#define Polygons 6
#define Angle 20
#define Sharp 3

//+++++++++++++++++++++++++++++++++
//	Image
//+++++++++++++++++++++++++++++++++
#define Contrast 1
#define Saturation 1
#define Brightness 1

//+++++++++++++++++++++++++++++++++
//	SMAA
//+++++++++++++++++++++++++++++++++
#define SMAA true
#define Threshold 0.05
#define Depth_Threshold 0.001
#define Max_Search_Steps 64
#define Max_Search_StepsDiag 16
#define Corner_Rounding 0
#define Edge_Detection_Mode 2
#define Predication 0
#define Predication_Threshold 0.001
#define Predication_Scale 2
#define Predication_Strength 0.4
#define Debug_Output 0

//+++++++++++++++++++++++++++++++++
//	FXAA
//+++++++++++++++++++++++++++++++++
#define FXAA false
#define Subpix 0.75
#define Edge_Threshold 0.18
#define Edge_Threshold_Min 0.18



#define ScreenPow float3(BUFFER_WIDTH,BUFFER_HEIGHT,BUFFER_RCP_WIDTH)
#define PixelPow float2(BUFFER_RCP_WIDTH,BUFFER_RCP_HEIGHT)
texture2D texColor:COLOR;
texture2D texDepth:DEPTH;
texture2D texHDR1{Width=BUFFER_WIDTH;Height=BUFFER_HEIGHT;Format=RGBA8;};
texture2D texHDR2{Width=BUFFER_WIDTH;Height=BUFFER_HEIGHT;Format=RGBA8;};
sampler2D SamplerColor{Texture=texColor;};
sampler2D SamplerDepth{Texture=texDepth;};
sampler2D SamplerHDR1{Texture=texHDR1;};
sampler2D SamplerHDR2{Texture=texHDR2;};
struct VS_OUTPUT_POST{float4 vpos:POSITION;float2 txcoord:TEXCOORD0;};
VS_OUTPUT_POST VS_PostProcess(in uint id:SV_VertexID){VS_OUTPUT_POST OUT;OUT.txcoord.x=(id==2)?2:0;OUT.txcoord.y=(id==1)?2:0;OUT.vpos=float4(OUT.txcoord*float2(2,-2)+float2(-1,1),0,1);return OUT;}
float Depth(float2 coords){float Qdepth=tex2Dlod(SamplerDepth,float4(coords.xy,0,0)).x;Qdepth=saturate(1f-Qdepth);Qdepth=(exp(pow(Qdepth,16f))-1)/8;return Qdepth;}
float GetCoC(float2 coords){float scenedepth=Depth(coords.xy);float scenefocus=0;float scenecoc=0;for(int r=0;r<2;r++){sincos(3.14159*r,coords.y,coords.x);coords.y*=ScreenPow.z;scenefocus+=Depth(coords*.004+float2(.5,.6).xy);}scenefocus/=2;scenefocus=smoothstep(0,1,scenefocus);scenedepth=smoothstep(0,1,scenedepth);float farBlurDepth=scenefocus*pow(4.0,Far);{scenecoc=(scenedepth-scenefocus)/(farBlurDepth-scenefocus);scenecoc=saturate(scenecoc);}return saturate(scenecoc*.5+.5);}
float3 BokehBlur(sampler2D tex,float2 coord,float CoC,float centerDepth){float4 res=float4(tex2Dlod(tex,float4(coord.xy,0,0)).xyz,1);int ringCount=round(lerp(1,(float)Quality,CoC/Pow));float2 discRadius=CoC*PixelPow.xy;float2 edgeVertices[Polygons+1];res.xyz=pow(res.xyz,Sharp)*res.w;for(int z=0;z<=Polygons;z++){sincos((6.2831853/Polygons)*z+radians(Angle),edgeVertices[z].y,edgeVertices[z].x);}for(float i=1;i<=ringCount;i++){for(int j=1;j<=Polygons;j++){float radiusCoeff=i/ringCount;float blursamples=i;for(float k=0;k<blursamples;k++){float2 sampleOffset=lerp(edgeVertices[j-1].xy,edgeVertices[j].xy,k/blursamples)*radiusCoeff;float4 tap=tex2Dlod(tex,float4(coord.xy+sampleOffset.xy*discRadius,0,0));tap.w=(tap.w>=centerDepth*.99)?1:pow(abs(tap.w*2-1),4);res.xyz+=pow(tap.xyz,Sharp)*tap.w;res.w+=tap.w;}}}res.xyz=max(res.xyz/res.w,0);return pow(res.xyz,1.0/Sharp);}
float4 PS_Focus(VS_OUTPUT_POST IN):COLOR{float4 scenecolor=tex2D(SamplerColor,IN.txcoord.xy);scenecolor.w=GetCoC(IN.txcoord.xy);return scenecolor;}
float4 PS_Bokeh(VS_OUTPUT_POST IN):COLOR{float4 scenecolor=tex2D(SamplerHDR1,saturate(IN.txcoord.xy));if(max(IN.txcoord.x,IN.txcoord.y)>1.05)discard;float centerDepth=scenecolor.w;float blurAmount=abs(centerDepth*2-1);float discRadius=blurAmount*Pow;discRadius*=(centerDepth<.5)?(1/max(4.0,1)):1;scenecolor.xyz=(discRadius>=1.2)?BokehBlur(SamplerHDR1,IN.txcoord.xy,discRadius,centerDepth):scenecolor.xyz;return scenecolor;}
float4 PS_Img(VS_OUTPUT_POST IN):COLOR{float4 scenecolor=tex2D(SamplerHDR2,IN.txcoord.xy);return scenecolor;}
float4 PS_Post(VS_OUTPUT_POST IN):COLOR{float4 scenecolor=tex2D(SamplerColor,IN.txcoord.xy);float4 blurcolor=.0001;float outOfFocus=abs(scenecolor.w);float2 blurmult=smoothstep(.3,.8,outOfFocus)*PixelPow.xy;float weights[2]={1,1};for(float x=-1;x<=1;x++)for(float y=-1;y<=1;y++){float2 offset=float2(x,y);float offsetweight=weights[abs(x)]*weights[abs(y)];blurcolor.xyz+=tex2Dlod(SamplerColor,float4(IN.txcoord.xy+offset.xy*blurmult,0,0)).xyz*offsetweight;blurcolor.w+=offsetweight;}scenecolor.xyz=blurcolor.xyz/blurcolor.w;return scenecolor;}
technique ADOF<bool enabled=DOF;toggle=0x10;>{
pass Focus{VertexShader=VS_PostProcess;PixelShader=PS_Focus;RenderTarget=texHDR1;}
pass Bokeh{VertexShader=VS_PostProcess;PixelShader=PS_Bokeh;RenderTarget=texHDR2;}
pass Img{VertexShader=VS_PostProcess;PixelShader=PS_Img;}
pass Post{VertexShader=VS_PostProcess;PixelShader=PS_Post;}}
float3 Color(float3 color){float3 image1=color;float3 image2=color;image1=lerp(dot(1,image1),image1,Saturation);image1=lerp(.33,image1,Contrast);image1*=Brightness;color=lerp(image1,image2,0);return color;}
float4 PS_Img2(VS_OUTPUT_POST IN):COLOR{float4 color=tex2D(SamplerHDR1,IN.txcoord.xy);color.xyz=Color(color.xyz);return color;}
#pragma message "edited by Quant"
#define FXAA_QUALITY__PRESET 9
#define FXAA_HLSL_3 1
#include"Fxaa3_11.h"
float3 FX(in float4 vpos:POSITION,in float2 txcoord:TEXCOORD0):SV_Target{float3 color=FxaaPixelShader(txcoord,SamplerColor,PixelPow,1,Subpix,Edge_Threshold,Edge_Threshold_Min).rgb;return color;}
technique FX<bool enabled=FXAA;>{pass FX{VertexShader=VS_PostProcess;PixelShader=FX;}}
#define SMAA_RT_METRICS float4(BUFFER_RCP_WIDTH,BUFFER_RCP_HEIGHT,BUFFER_WIDTH,BUFFER_HEIGHT)
texture texEdges{Width=BUFFER_WIDTH;Height=BUFFER_HEIGHT;Format=RGBA8;};
texture texBlend{Width=BUFFER_WIDTH;Height=BUFFER_HEIGHT;Format=RGBA8;};
texture texArea<string source="AreaTex.dds";>{Width=160;Height=560;Format=R8G8;};
texture texSearch<string source="SearchTex.dds";>{Width=64;Height=16;Format=R8;};
sampler SamplerEdges{Texture=texEdges;};
sampler SamplerBlend{Texture=texBlend;};
sampler SamplerArea{Texture=texArea;};
sampler SamplerSearch{Texture=texSearch;};
#define SMAA_HLSL_3 1
#include"SMAA.hlsl"